package com.ccs.loas;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

public class View_Loa_Admin {

	public void go_to_Loa(WebDriver driver){
		driver.findElement(By.xpath("//a[contains(., 'View LOA')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void search_loa(WebDriver driver, String loa){
		loa = loa.toUpperCase().toString();
		Select status = new Select(driver.findElement(By.xpath("//select[@id='loaStatus']")));
		status.selectByVisibleText("ALL");
		driver.findElement(By.xpath("//input[@id='slcGuarantor']")).sendKeys("COCOLIFE HEALTH CARE");
		driver.findElement(By.xpath("//input[@id='loa-number']")).sendKeys(loa);
		driver.findElement(By.xpath("//input[@value='Search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void verify_loa(WebDriver driver, String status, String loaNo){

		String admDate = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[1]")).getText();
		String patientName = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[2]")).getText();
		String confNumber = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[3]")).getText();
		String pin = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[4]")).getText();
		String loaNumber = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[5]")).getText();
		String loaStatus = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[6]")).getText();
		
		if(admDate.isEmpty() || patientName.isEmpty() || confNumber.isEmpty() || pin.isEmpty()){
			System.out.println("Empty String found!");
		}
		
		boolean loaNo_checker, status_checker;
		if(status.equals("Approved")||status.equals("SubmittedtoSLMC")){
			loaNo = loaNo.toUpperCase().toString();
			if(loaNumber.equals(loaNo)){
				System.out.println("LOA Number is " +loaNumber+". CORRECT");
			}else{
				System.out.println("Search output of LOA is INCORRECT");
			}
			loaNo_checker = verify_loa_number(loaNumber);
			status_checker = verify_loa_status(loaStatus, status);
			if(loaNo_checker==true && status_checker==true){
				System.out.println("LOA Number and Status are CORRECT");
			}else{
				System.out.println("LOA Number and/or Status are INCORRECT");
			}
	
		}else{
	
			loaNo_checker = verify_loa_number(loaNumber);
			status_checker = verify_loa_status(loaStatus, status);
			if(loaNo_checker==false && status_checker==true){
				System.out.println("LOA Number and Status are CORRECT");
			}else{
				System.out.println("LOA Number and/or Status are INCORRECT");
			}
			driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/img"));
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
		
		if(status.equals("Approved")){
			try{
				String newText = driver.findElement(By.xpath("//div[@id='mainContent']/div[5]/div/table/tbody/tr/td[2]/font/strong")).getText();
				System.out.println("Text next to Patient Name if Loa is Approve: "+newText +". CORRECT");
			}catch(Exception e){
				System.out.println("NEW label next to Patient Name is not present. INCORRECT");
			}

			driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/a")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
	}
	
	public boolean verify_loa_number(String loaNo){
		
		if(loaNo.isEmpty()||loaNo==""){
			return false;
		}else{
			return true;
		}
	}
	
	public boolean verify_loa_status(String loaStatus, String status){
		
		loaStatus = loaStatus.replaceAll(" ", "");
		if(loaStatus.equals(status)){
			return true;
		}else{
			return false;
		}
	}
	
	
}
